package com.hjm.project.walletDetails.walletDetails.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.agentInfo.agentInfo.service.IAgentInfoService;
import com.hjm.project.walletDetails.walletDetails.domain.PaymentExport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.walletDetails.walletDetails.domain.WalletDetails;
import com.hjm.project.walletDetails.walletDetails.service.IWalletDetailsService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 服务商钱包明细Controller
 * 
 * @author WalletDetails
 * @date 2020-11-25
 */
@Controller
@RequestMapping("/walletDetails/walletDetails")
public class WalletDetailsController extends BaseController
{
    private String prefix = "walletDetails/walletDetails";

    @Autowired
    private IWalletDetailsService walletDetailsService;
    @Resource
    private IAgentInfoService agentInfoService;

    @RequiresPermissions("WalletDetails:WalletDetails:view")
    @GetMapping()
    public String walletDetails() {
        return prefix + "/walletDetails";
    }


    /**
     * 查询服务商钱包明细列表
     */
    @RequiresPermissions("WalletDetails:WalletDetails:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WalletDetails walletDetails)
    {
        startPage();
        if (ShiroUtils.isAgent()) {
            walletDetails.setLoginAgentNum(ShiroUtils.getLoginAgentNum());
        }
        List<WalletDetails> list = walletDetailsService.selectwalletDetailsList(walletDetails);
        return getDataTable(list);
    }

    @GetMapping("/agentAccount")
    public String agentAccount() {
        return prefix + "/agentAccount";
    }

    @PostMapping("/agentAccountList")
    @ResponseBody
    public TableDataInfo agentAccountList(@RequestParam Map params) {
        if (ShiroUtils.isAgent()) {
            params.put("loginAgentNum", ShiroUtils.getLoginAgentNum());
        }

        Map map = walletDetailsService.agentAccountListSum(params);
        startPage();

        List<Map> maps = walletDetailsService.agentAccountList(params);
        return getDataTable(maps,map);
    }

    /**
     * 前往设置业务类型页面
     */
    @GetMapping("/accountUpdate")
    public String accountUpdate(@RequestParam Map params, ModelMap mmp) {
        Map map = walletDetailsService.agentAccountList(params).get(0);
        mmp.put("account",map);
        return prefix + "/accountUpdate";
    }

    @Log(title = "变更代理账户状态", businessType = BusinessType.UPDATE)
    @PostMapping("/saveAccountUpdate")
    @ResponseBody
    public AjaxResult saveAccountUpdate(@RequestParam Map params) {
        int i = walletDetailsService.saveAccountUpdate(params);
        return toAjax(i);
    }

    @GetMapping("/accountAdjust")
    public String accountAdjust(@RequestParam Map params, ModelMap mmp) {
        Map map = walletDetailsService.agentAccountList(params).get(0);
        mmp.put("account",map);
        return prefix + "/accountAdjust";
    }

    @Log(title = "代理账户调增/调减", businessType = BusinessType.UPDATE)
    @PostMapping("/saveAccountAdjust")
    @ResponseBody
    public AjaxResult saveAccountAdjust(@RequestParam Map params) {
        AjaxResult ajaxResult = walletDetailsService.saveAccountAdjust(params);
        return ajaxResult;
    }


    /**
     * 导出服务商钱包明细列表
     */
    @RequiresPermissions("WalletDetails:WalletDetails:export")
    @Log(title = "服务商钱包明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WalletDetails walletDetails)
    {
        List<WalletDetails> list = walletDetailsService.selectwalletDetailsList(walletDetails);
        ExcelUtil<WalletDetails> util = new ExcelUtil<WalletDetails>(WalletDetails.class);
        return util.exportExcel(list, "WalletDetails");
    }


    /**
     * 新增服务商钱包明细
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存服务商钱包明细
     */
    @RequiresPermissions("WalletDetails:WalletDetails:add")
    @Log(title = "服务商钱包明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WalletDetails walletDetails)
    {
        return toAjax(walletDetailsService.insertwalletDetails(walletDetails));
    }

    /**
     * 修改服务商钱包明细
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WalletDetails walletDetails = walletDetailsService.selectwalletDetailsById(id);
        mmap.put("WalletDetails", walletDetails);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务商钱包明细
     */
    @RequiresPermissions("WalletDetails:WalletDetails:edit")
    @Log(title = "服务商钱包明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WalletDetails walletDetails)
    {
        return toAjax(walletDetailsService.updatewalletDetails(walletDetails));
    }

    /**
     * 删除服务商钱包明细
     */
    @RequiresPermissions("WalletDetails:WalletDetails:remove")
    @Log(title = "服务商钱包明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(walletDetailsService.deletewalletDetailsByIds(ids));
    }

    @RequiresPermissions("WalletDetails:WalletDetails:payment")
    @GetMapping("/payment")
    public String payment(ModelMap mmap)
    {
        Calendar calendar1 = Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        calendar1.add(Calendar.DATE, -1);
        String date = sdf1.format(calendar1.getTime());
        mmap.put("beginTime", date);
        mmap.put("endTime", date);
        return prefix + "/payment";
    }

    @RequiresPermissions("WalletDetails:WalletDetails:paymentSuccess")
    @GetMapping("/paymentSuccess")
    public String paymentSuccess()
    {
        return prefix + "/paymentSuccess";
    }

    @RequiresPermissions("WalletDetails:WalletDetails:paymentAll")
    @GetMapping("/paymentAll")
    public String paymentAll()
    {
        return prefix + "/paymentAll";
    }

    /**
     * 查询服务商钱包明细列表
     */
    @RequiresPermissions("WalletDetails:WalletDetails:paymentSuccessList")
    @PostMapping("/paymentSuccessList")
    @ResponseBody
    public TableDataInfo paymentSuccessList(WalletDetails walletDetails)
    {
        startPage();
        walletDetails.setStatus("1");
        walletDetails.setOrderstate("4");
        List<WalletDetails> list = walletDetailsService.selectPaymentList(walletDetails);
        return getDataTable(list);
    }

    /**
     * 查询服务商钱包明细列表
     */
    @RequiresPermissions("WalletDetails:WalletDetails:paymentList")
    @PostMapping("/paymentList")
    @ResponseBody
    public TableDataInfo paymentList(WalletDetails walletDetails)
    {
        startPage();
        walletDetails.setStatus("3");
        walletDetails.setOrderstate("1");
        List<WalletDetails> list = walletDetailsService.selectPaymentList(walletDetails);
        return getDataTable(list);
    }

    /**
     * 查询服务商钱包明细列表
     */
    @RequiresPermissions("WalletDetails:WalletDetails:paymentAllList")
    @PostMapping("/paymentAllList")
    @ResponseBody
    public TableDataInfo paymentAllList(WalletDetails walletDetails)
    {
        startPage();
        List<WalletDetails> list = walletDetailsService.selectPaymentList(walletDetails);
        return getDataTable(list);
    }

    /**
     * 导出服务商钱包明细列表
     */
    @RequiresPermissions("WalletDetails:WalletDetails:exportPayment")
    @Log(title = "服务商钱包明细", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPayment")
    @ResponseBody
    public AjaxResult exportPayment(WalletDetails walletDetails) throws Exception {
        List<PaymentExport> list = walletDetailsService.exportPaymentList(walletDetails);
        ExcelUtil<PaymentExport> util = new ExcelUtil<>(PaymentExport.class);
        return util.exportExcel(list, "WalletDetails");
    }

    /**
     * 标记失败
     */
    @RequiresPermissions("walletDetails:walletDetails:markingFailed")
    @Log(title = "服务商钱包明细", businessType = BusinessType.UPDATE)
    @PostMapping( "/markingFailed")
    @ResponseBody
    public AjaxResult markingFailed(String ids)
    {
        return toAjax(walletDetailsService.markingFailed(ids));
    }
    /**
     * 标记失败
     */
    @Log(title = "服务商钱包明细", businessType = BusinessType.UPDATE)
    @PostMapping( "/updateAgentAccount")
    @ResponseBody
    public AjaxResult updateAgentAccount(@RequestParam Map<String,String> params) {
        Map update = new HashMap();
        update.put("agent_num",params.get("agent_num"));
        if ("1".equals(params.get("statusType"))) {
            update.put("acc_status1",params.get("status"));
        } else if ("2".equals(params.get("statusType"))) {
            update.put("acc_status2",params.get("status"));
        } else if ("3".equals(params.get("statusType"))) {
            update.put("acc_status3",params.get("status"));
        } else if ("4".equals(params.get("statusType"))) {
            update.put("acc_status4",params.get("status"));
        }
        int i = walletDetailsService.updateAgentAccount(update);
        return toAjax(i);
    }
}
